package com.masu.knows.sys.controller;

import com.masu.knows.commons.model.Permission;
import com.masu.knows.commons.model.Role;
import com.masu.knows.commons.model.User;
import com.masu.knows.sys.mapper.UserMapper;
import com.masu.knows.sys.service.IUserService;
import com.masu.knows.sys.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 沈传尚
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IUserService userService;

    @GetMapping("/permission/{id}")
    public List<Permission> getPermissions(@PathVariable(value = "id") Integer id){
        return userMapper.getPermissionsByUserId(id);
    }

    @GetMapping("/info")
    @PreAuthorize("hasAnyAuthority('get')")  //设置权限资格 必须拥有'get'权限才可以访问 不设置默认不需要权限
    public String info(){
        return "拥有get资格";
    }

    @GetMapping("/master")
    public List<User> findTeachers(){
       return userService.findAllTeacher();
    }

    // 更具当前登录用户查询用户信息面板的信息
    @GetMapping(value = "/me")
    public UserVo getUserVo(@AuthenticationPrincipal UserDetails userDetails){
       return  userService.getUserVO(userDetails.getUsername());
    }












}
